<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>宠物申请列表</title>
    <script src="/js/global.js"></script>
    <link rel="stylesheet" href="/css/font.css" type="text/css"/>
    <link rel="stylesheet" href="/css/layui.css" type="text/css"/>
    <link rel="stylesheet" href="/css/xadmin.css" type="text/css"/>
    <script type="text/javascript" src="/js/layui.js" ></script>
    <script type="text/javascript" src="/js/xadmin.js" ></script>
    <script type="text/javascript" src="/js/jquery-3.5.1.min.js" ></script>
</head>
<body>
<!-- 条件查询 -->
<div class="layui-form-item" style="margin-top: 20px;" onsubmit="false">
    <div class="layui-form-item">

        <label class="layui-form-label">用户名称</label>
        <div class="layui-input-inline" style="width: 200px;">
            <input type="text" name="realname" id="realname" placeholder="用户名称" class="layui-input">
        </div>

        <label class="layui-form-label">宠物名称</label>
        <div class="layui-input-inline" style="width: 200px;">
            <input type="text" name="petsName" id="petsName" placeholder="宠物名称" class="layui-input">
        </div>

        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline layui-form" style="width: 200px;">
            <select name="status" id="status" lay-verify="" lay-search="">
                <option value="0">全部</option>
                <option value="1">待处理</option>
                <option value="2">批准</option>
                <option value="3">拒绝</option>
            </select>
        </div>

        <button class="layui-btn" id="search" data-type="reload">查询</button>

    </div>
</div>

<!-- 表格 -->
<table id="test" class="layui-hide" lay-filter="test"></table>

<!-- 行内 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-icon" lay-event="updateTy"><i class="layui-icon">&#xe642;</i>同意</a>
    <a class="layui-btn layui-btn-xs layui-icon  layui-btn-danger" lay-event="updateJj"><i class="layui-icon">&#xe642;</i>拒绝</a>
</script>

<script>
    layui.use(['layer', 'table', 'form', 'transfer', 'util', 'laydate'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var transfer = layui.transfer;
        var util = layui.util;
        var laydate = layui.laydate;

        //初始化表格数据
        table.render({
            elem: '#test',
            url: '../adoption/findAll',
            method: "get",
            title: '宠物申请列表',
            page : true,
            id: 'testReload',
            //修改响应数据
            parseData: function(res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.count, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [
                [{
                    field: 'adoptionid',
                    title: 'ID',
                    align: 'center'
                },
                    {
                        field: 'realname',
                        title: '用户名称',
                        align: 'center'
                    },        {
                    field: 'address',
                    title: '用户地址',
                    align: 'center'
                }, {
                    field: 'synopsis',
                    title: '用户简介',
                    align: 'center'
                },  {
                    field: 'petsName',
                    title: '宠物名称',
                    align: 'center'
                },   {
                    field: 'description',
                    title: '宠物简介',
                    align: 'center'
                },    {
                    field: 'typeName',
                    title: '宠物分类',
                    align: 'center'
                },{
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    templet: function (d) {
                        if (d.status === 1) {
                            return '<span style="color: #FF5722">待处理</span>';
                        } else if (d.status === 2) {
                            return '<span style="color: #19be6b">已同意</span>';
                        } else if (d.status === 3) {
                            return '<span style="color: #FF5722">已拒绝</span>';
                        }
                    }
                },{
                    field: 'likenum',
                    title: '喜欢人数',
                    align: 'center'
                },
                    {
                        fixed: 'right',
                        title: '操作',
                        toolbar: '#barDemo',
                        width: 180,
                        align: 'center'
                    }
                ]
            ]
        });

        //模糊查询
        $('#search').on('click', function() {
            table.reload('testReload', {
                where: {
                    'petsName': $("#petsName").val(),
                    'realname': $("#realname").val(),
                    'status': $("#status").val(),
                }
            });
        });

        //重新加载
        function back() {
            table.reload('testReload', {
                page: {
                    curr: $(".layui-laypage-em").next().html()
                }
            });
        }

        //监听行工具事件
        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if (obj.event === 'updateTy') {
                if (obj.data.status===2){
                    layer.msg('该用户已同意过，不能重复同意');
                    return false;
                }else if (obj.data.status===3){
                   let indexConfirm = layer.confirm('该用户已拒绝过，请确定要同意吗？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                       layer.close(indexConfirm); // 关闭确认框
                        layer.open({
                            title: '您正在同意名称为：' + obj.data.realname + '的申请信息',
                            type: 2,
                            shade: 0.3,
                            shadeClose: true,
                            skin: 'layui-layer-molv',
                            anim: 5,
                            btnAlign: 'c',
                            closeBtn: 1,
                            area: [($(window).width() - 50) + "px", ($(window).height() - 50) + "px"],
                            content: 'agreeOrReject.html',
                            success: function(layero, index) {
                                var body = layer.getChildFrame('body', index);
                                body.find("#adoptionid").val(obj.data.adoptionid);
                                body.find("#status").val(2);
                            },
                            end: function() {
                                back();
                            }
                        });
                    })
                }else{
                    layer.open({
                        title: '您正在同意名称为：' + obj.data.realname + '的申请信息',
                        type: 2,
                        shade: 0.3,
                        shadeClose: true,
                        skin: 'layui-layer-molv',
                        anim: 5,
                        btnAlign: 'c',
                        closeBtn: 1,
                        area: [($(window).width() - 50) + "px", ($(window).height() - 50) + "px"],
                        content: 'agreeOrReject.html',
                        success: function(layero, index) {
                            var body = layer.getChildFrame('body', index);
                            body.find("#adoptionid").val(obj.data.adoptionid);
                            body.find("#status").val(2);
                        },
                        end: function() {
                            back();
                        }
                    });
                }
            }else if (obj.event === 'updateJj') {
                if (obj.data.status===3){
                    layer.msg('该用户已拒绝过，不能重复拒绝');
                    return false;
                }else if (obj.data.status===2){
                   let indexConfirm= layer.confirm('该用户已同意过，请确定要拒绝吗？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        layer.close(indexConfirm); // 关闭确认框
                        layer.open({
                            title: '您正在拒绝名称为：' + obj.data.realname + '的申请信息',
                            type: 2,
                            shade: 0.3,
                            shadeClose: true,
                            skin: 'layui-layer-molv',
                            anim: 5,
                            btnAlign: 'c',
                            closeBtn: 1,
                            area: [($(window).width() - 50) + "px", ($(window).height() - 50) + "px"],
                            content: 'agreeOrReject.html',
                            success: function(layero, index) {
                                var body = layer.getChildFrame('body', index);
                                body.find("#adoptionid").val(obj.data.adoptionid);
                                body.find("#status").val(3);
                            },
                            end: function() {
                                back();
                            }
                        });
                    })
                    //关闭询问框
                    var index = parent.layer.getFrameIndex(window
                        .name); //获取当前窗口的name
                    parent.layer.close(index); //关闭窗口
                }else{
                    layer.open({
                        title: '您正在拒绝名称为：' + obj.data.realname + '的申请信息',
                        type: 2,
                        shade: 0.3,
                        shadeClose: true,
                        skin: 'layui-layer-molv',
                        anim: 5,
                        btnAlign: 'c',
                        closeBtn: 1,
                        area: [($(window).width() - 50) + "px", ($(window).height() - 50) + "px"],
                        content: 'agreeOrReject.html',
                        success: function(layero, index) {
                            var body = layer.getChildFrame('body', index);
                            body.find("#adoptionid").val(obj.data.adoptionid);
                            body.find("#status").val(3);
                        },
                        end: function() {
                            back();
                        }
                    });
                }
            }
        });

    });
</script>
</body>
</html>
